package com.disdr.picture;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class 过磅单填充 {

    static Set<String> nameSet = new HashSet<String>();
    public static void main(String[] args) throws IOException {

        String 石头人 = "2022-12-22,湘N0T972,煤制品,30.00;2022-12-15,吉BE3948,煤制品,30.00;2022-12-19,黑MN6518,煤制品,32.00;2022-12-11,鲁Q295KJ,煤制品,30.00;2022-12-18,辽F30947,煤制品,35.00;2022-12-15,冀E2588Y,煤制品,30.00;2022-12-15,吉AW7489,煤制品,30.00;2022-12-19,吉BE3948,煤制品,32.00;2022-12-15,冀D3B961,煤制品,30.00;2022-12-10,吉AW7489,煤制品,30.00;2022-12-19,吉BE3728,煤制品,30.00;2022-12-19,冀J1Y926,煤制品,35.00;2022-12-11,黑FB9815,煤制品,30.00;2022-12-14,鲁RM7935,煤制品,30.00;2022-12-13,湘N97976,煤制品,30.00;2022-12-17,川ABY006,煤制品,30.00;2022-12-18,皖SD9407,煤制品,30.00;2022-12-17,浙G72079,煤制品,30.00;2022-12-09,冀J2P057,煤制品,30.00;2022-12-19,苏GP9673,煤制品,30.00";
        for(String str : 石头人.split(";")){
            generateReceipt(str.split(","));
        }

    }

    /**
     * 环运回单
     *
     * @return
     * @throws IOException
     */
    public static BufferedImage generateReceipt(String[] obj) throws IOException {

        //BufferedImage bi = ImageIO.read(new File("F://gbd.jpg"));
        File file = new File("src\\main\\resources\\templates\\gbd.jpg");
        //System.out.println(file.getAbsolutePath());
        BufferedImage bi = ImageIO.read(file);
        //System.out.println(bi.getWidth());//841
        //System.out.println(bi.getHeight());//332

        // 得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        Font font = new Font("宋体", 0, 25);
        g2.setFont(font);
        g2.setColor(Color.GRAY);// 设置字体颜色

        Date dd = DateUtil.parse(obj[0]);
        System.out.println(dd.getTime()/10000);
        String[] date = obj[0].split(" ");
        g2.drawString(date[0], 210, 92);
        //g2.drawString(date[1], 550, 92);
        g2.drawString(obj[1], 210, 127);
        g2.drawString("金世通", 210, 162);
        //g2.drawString("19500", 220, 197);
        //g2.drawString(".getAdvance()", 210, 232);
        //g2.drawString(".getAdvance()", 210, 267);

        //g2.drawString(obj[1].substring(11), 550, 92);
        g2.drawString(obj[2], 550, 127);
        //g2.drawString("57380", 560, 162);
        //Integer ii = 28000 + (int)(Math.random()*8000);
        if(Validator.isNumber(obj[3])){
            BigDecimal bigDecimal = new BigDecimal(obj[3]);
            if(bigDecimal.compareTo(new BigDecimal(50))==1){
                g2.drawString(obj[3], 560, 197);
            }else {
                g2.drawString(bigDecimal.multiply(new BigDecimal(1000)).toString(), 560, 197);
            }
        }else{
            g2.drawString(obj[3], 560, 197);
        }
        //g2.drawString("24", 550, 232);
        //g2.drawString("", 550, 267);

        g2.drawString("磅A01", 195, 310);
        g2.dispose();//释放对象
        String name = obj[1]+"_"+date[0].replaceAll("\\/","-");
        String nn = name;
        int i = 1;
        while (过磅单填充.nameSet.contains(nn)){
            nn = name+"_"+i;
            i++;
        }
        过磅单填充.nameSet.add(nn);
        nn = nn +".jpg";
        ImageIO.write(bi, "jpg", new FileOutputStream("D:\\user\\Desktop\\金世通回单3\\"+nn));// 保存图片 JPEG表示保存格式
        return bi;

    }


}
